"""
@file: goods.py
@author: yangsiting
@data: 2021/04/22
@desc:
"""
from flask import request, g
from libs.nestable_blueprint import NestableBlueprint
from flask_restful import Api, Resource
from libs.handler import default_error_handler
from models.goods import Goods_info,Goods_detail
from models.base import db
from libs.response import generate_response
from serializer.goods import good_schema,goods_detail_schema,goods_schema
from forms.user import RegisterForm, LoginForm
from libs.error_code import FormValidateException, ArgsTypeException
from libs.authorize import auth

good_bp = NestableBlueprint('goods_v1', __name__, url_prefix='goods/')
api = Api(good_bp)

# 指定当出现异常时，所调用的处理方法
# 当需要看到真实的错误信息时，请把下面这一句注释掉
#api.handle_error = default_error_handler

class GoodsView(Resource):
    #@auth.login_required
    def get(self):
        goods = Goods_info.query.all()
        goods_dict = goods_schema.dump(goods)
        # 编写函数来进行序列化？ => 这一部分大家可以尽情优化~~
        return generate_response(data=goods_dict)
        # return generate_response(data=servers_schema.dump(servers))

    @auth.login_required
    def post(self):
        pass
        # 业务逻辑

    def put(self):
        pass

    def patch(self):
        pass


class GoodsDetailsView(Resource):
    def get(self,goods_id):
        goods = Goods_info.query.filter(Goods_info.goods_id == goods_id).all()
        goods_detail = Goods_detail.query.filter(Goods_detail.goods_id == goods_id).all()
        goods_dict = goods_schema.dump(goods)
        goods_detail_dict = goods_detail_schema.dump(goods_detail)
        # 编写函数来进行序列化？ => 这一部分大家可以尽情优化~~
        return generate_response(data=(goods_dict,goods_detail_dict))
        # return generate_response(data=servers_schema.dump(servers))



class Goods_category1_View(Resource):
    def get(self,goods_category):
        goods = Goods_info.query.filter(Goods_info.goods_category == goods_category).all()
        goods_dict = goods_schema.dump(goods)
        return  generate_response(data=goods_dict)

# 注册路由
api.add_resource(GoodsView, '/goods_info/')
api.add_resource(GoodsDetailsView,'/goods_detail/<goods_id>/')
api.add_resource(Goods_category1_View,'/goods_category/<goods_category>/')
